from contextlib import asynccontextmanager

from fastapi import FastAPI

from .db import create_db_and_tables
from .routes import router


@asynccontextmanager
async def lifespan(app: FastAPI):
    # 启动时连接数据库
    create_db_and_tables()
    yield
    # 关闭时断开数据库连接


app = FastAPI(lifespan=lifespan)

# 注册路由
app.include_router(router)
